#region using
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using BLToolkit.Mapping;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using BLToolkit.EditableObjects;
#endregion

namespace Brainsoft.Core.BusinessLogic
{
    /// <summary>
    /// Este objeto representa las propiedades y metodos de ApplicationItems.
    /// Nomenclatura a usar para los SP = ApplicationItem_SelectByKey , ApplicationItem_Update, etc.
    /// En caso contrario usar el nombre de los SP como atributos de la clase, ej:
    ///[ActionSprocName("SelectAll", "NombreProcedimiento")], [ActionSprocName("Insert", "NombreProcedimiento")], etc.
    /// </summary>	

    public class ApplicationItems
    {

        #region Constructors
        public ApplicationItems()
        {
        }
        #endregion

        #region Private Properties
        private int _applicationItemID;
        private int _applicationID;
        private string _description = String.Empty;
        private string _targetUrl = String.Empty;
        private int _sortOrder;
        private bool _visible;
        private bool _markAsNew;
        private bool _hasRealItem;
        private string _keyWords = String.Empty;
        #endregion

        #region Public Properties
        public int ApplicationItemID
        {
            get { return _applicationItemID; }
            set { _applicationItemID = value; }
        }

        public int ApplicationID
        {
            get { return _applicationID; }
            set { _applicationID = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string TargetUrl
        {
            get { return _targetUrl; }
            set { _targetUrl = value; }
        }

        public int SortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }

        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }

        public bool MarkAsNew
        {
            get { return _markAsNew; }
            set { _markAsNew = value; }
        }

        public bool HasRealItem
        {
            get { return _hasRealItem; }
            set { _hasRealItem = value; }
        }

        public string KeyWords
        {
            get { return _keyWords; }
            set { _keyWords = value; }
        }
        #endregion

        #region Relational Properties

        #endregion

        #region Generic Methods

        #region Insert, Update, Delete

        public void Insert(ApplicationItems obj)
        {
            Query.Insert(db, obj);
        }

        public void Update(ApplicationItems obj)
        {
            Query.Update(db, obj);
        }

        public void Delete(ApplicationItems obj)
        {
            Query.Delete(db, obj);
        }

        public void Delete(int id)
        {
            Query.DeleteByKey(db, id);
        }

        #endregion

        #region Select

        public List<ApplicationItems> SelectAll()
        {
            return Query.SelectAll(db);
        }

        public ApplicationItems SelectByKey(object[] parameters)
        {
            return Query.SelectByKey(db, parameters);
        }

        #endregion

        #region Batch
        public bool BatchInsert(List<ApplicationItems> list)
        {
            using (DbManager db = new DbManager())
            {
                db.BeginTransaction();
                try
                {
                    foreach (ApplicationItems obj in list)
                    {
                        obj.Insert(obj);
                    }
                }
                catch
                {
                    db.RollbackTransaction();
                    return false;
                }
                db.CommitTransaction();
                return true;
            }
        }
        public bool BatchUpdate(List<ApplicationItems> list)
        {
            using (DbManager db = new DbManager())
            {
                db.BeginTransaction();
                try
                {
                    foreach (ApplicationItems obj in list)
                    {
                        obj.Update(obj);
                    }
                }
                catch 
                {
                    db.RollbackTransaction();
                    return false;
                }
                db.CommitTransaction();
                return true;
            }
        }
        #endregion

        #endregion

        #region Custom Methods
        public List<ApplicationItems> GetByAppId(int ApplicationId)
        {
            using (db = new DbManager("Connection String"))
            {
                return db.SetSpCommand("ApplicationItems_SelectByApplicationId"
                       , db.Parameter("@ApplicationId", ApplicationId))
                       .ExecuteList<ApplicationItems>();
            }
        }       
        public int GetNewId()
        {
            using (db = new DbManager("Connection String"))
            {
                int Id = Convert.ToInt32(db.SetCommand("SELECT MAX(ApplicationItemID) + 1 FROM ApplicationItems").ExecuteScalar());

                db.Close();

                return Id;
            }
        }
        #endregion

        #region Protected Members

        protected DbManager db;

        private SprocQuery<ApplicationItems> _query;
        protected virtual SprocQuery<ApplicationItems> Query
        {
            get
            {
                if (null == _query)
                    db = new DbManager("Connection String");//ingresar aqu? el Connection String
                _query = new SprocQuery<ApplicationItems>(db);
                return _query;
            }
        }

        #endregion
    }
}
